﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PulseLibrary;
using PulseLibrary.GUIControls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace PulsePrototype.GameStates
{
    public class StartMenuScreen : BaseGameState
    {
        private PictureBox background;
        private PictureBox selectGem;
        private LinkLabel startGame;
        private LinkLabel loadGame;
        private LinkLabel exitGame;
        private float maxItemWidth = 0f;

        public StartMenuScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            ContentManager Content = Game.Content;
            Texture2D gemTexture = Content.Load<Texture2D>(@"GUI\gem");
            Texture2D backgroundImage = Content.Load<Texture2D>(@"Background\titlescreen");
            Rectangle gemRectangle = new Rectangle(0, 0, gemTexture.Width, gemTexture.Height);

            background = new PictureBox(backgroundImage, GameRef.ScreenRectangle);
            selectGem = new PictureBox(gemTexture, gemRectangle);

            startGame = new LinkLabel();
            startGame.Text = "Create New Game";
            startGame.Color = Color.Black;
            startGame.Size = startGame.SpriteFont.MeasureString(startGame.Text);
            startGame.Selected += new EventHandler(menuItem_Selected);

            loadGame = new LinkLabel();
            loadGame.Text = "Continue Game";
            loadGame.Color = Color.Black;
            loadGame.Size = loadGame.SpriteFont.MeasureString(loadGame.Text);
            loadGame.Selected += menuItem_Selected;

            exitGame = new LinkLabel();
            exitGame.Text = "Exit Game";
            exitGame.Color = Color.Black;
            exitGame.Size = exitGame.SpriteFont.MeasureString(exitGame.Text);
            exitGame.Selected += menuItem_Selected;

            controlManager.Add(background);
            controlManager.Add(selectGem);
            controlManager.Add(startGame);
            controlManager.Add(loadGame);
            controlManager.Add(exitGame);

            controlManager.NextControl();

            controlManager.FocusChanged += new EventHandler(ControlManager_FocusChanged);
            Vector2 position = new Vector2(GameRef.ScreenRectangle.Center.X - loadGame.Size.X/2,
                GameRef.ScreenRectangle.Center.Y + 110);

            foreach (Control c in controlManager)
            {
                if (c is LinkLabel)
                {
                    if (c.Size.X > maxItemWidth)
                        maxItemWidth = c.Size.X;
                    c.Position = position;
                    position.Y += c.Size.Y + 5f;
                }
            }
            ControlManager_FocusChanged(startGame, null);
        }

        void ControlManager_FocusChanged(object sender, EventArgs e)
        {
            Control control = sender as Control;
            Vector2 position = new Vector2(control.Position.X + maxItemWidth + 10f,
                control.Position.Y);
            selectGem.SetPosition(position);
        }

        private void menuItem_Selected(object sender, EventArgs e)
        {
            // Not sure if this is the best way to start a "new" level.
            if (sender == startGame)
            {
                GameRef.GamePlayScreen1 = new GamePlayScreen(GameRef, stateManager);
                stateManager.PushState(GameRef.GamePlayScreen1);
            }
            else if (sender == loadGame)
            {
                stateManager.PushState(GameRef.GamePlayScreen1);
            }

            if (sender == exitGame)
            {
                GameRef.Exit();
            }
        }

        public override void Update(GameTime gameTime)
        {
            controlManager.Update(gameTime, playerIndexInControl);
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();

            base.Draw(gameTime);
            controlManager.Draw(GameRef.SpriteBatch);
            
            GameRef.SpriteBatch.End();
        }
    }
}
